package cn.it.ygh.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.it.ygh.mapper.HouseDealMapper;
import cn.it.ygh.pojo.Apply;
import cn.it.ygh.pojo.HouseInfo;
import cn.it.ygh.pojo.Page;
import cn.it.ygh.pojo.Renter;
import cn.it.ygh.pojo.User;
import cn.it.ygh.service.HouseDealService;

@Service("houseDealService")
public class HouseDealServiceImpl implements HouseDealService {
	@Resource
	private HouseDealMapper houseDealMapper;

	public List<HouseInfo> getHouseInfo(int minPrice, int maxPrice, int minArea, int maxArea, int layout) {
		return houseDealMapper.getHouseInfo(minPrice, maxPrice, minArea, maxArea, layout);
	}

	public int selectCount(Map<String, Object> map) {
		return houseDealMapper.selectCount(map);
	}
	
	public int selectCount1(Map<String, Object> map) {
		return houseDealMapper.selectCount1(map);
	}
	
	public int selectCount2(Map<String, Object> map) {
		return houseDealMapper.selectCount(map);
	}
	public int selectCount3(Map<String, Object> map) {
		return houseDealMapper.selectCount(map);
	}

	public Page<HouseInfo> findByPage(int currentPage, int minPrice, int maxPrice, int minArea, int maxArea,
			int layout,String keyword) {
		Map<String, Object> map = new HashMap<String, Object>();
		Page<HouseInfo> page = new Page<HouseInfo>();

		// 封装当前类
		page.setCurrentPage(currentPage);

		// 每页显示的数据
		int pageSize = 5;
		page.setPageSize(pageSize);
		Map<String, Object> con = new HashMap<String, Object>();
		con.put("minPrice", minPrice);
		con.put("maxPrice", maxPrice);
		con.put("minArea", minArea);
		con.put("maxArea", maxArea);
		con.put("layout", layout);
		con.put("keyword", keyword);
	
		// 封装总记录数
		int totalCount = houseDealMapper.selectCount(con);
		page.setTotalCount(totalCount);

		// 封装总页数
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);// 向上取整
		page.setTotalPage(num.intValue());

		map.put("start", (currentPage - 1) * pageSize);
		map.put("size", page.getPageSize());
		map.put("minPrice", minPrice);
		map.put("maxPrice", maxPrice);
		map.put("minArea", minArea);
		map.put("maxArea", maxArea);
		map.put("layout", layout);
		map.put("keyword", keyword);
		
		System.out.println(con);
		// 封装每页显示的数据
		List<HouseInfo> lists = houseDealMapper.findByPage(map);
		page.setLists(lists);

		return page;
	}

	public Page<HouseInfo> findByPage1(int currentPage, int minPrice, int maxPrice,int layout,String keyword) {
		Map<String, Object> map = new HashMap<String, Object>();
		Page<HouseInfo> page = new Page<HouseInfo>();

		// 封装当前类
		page.setCurrentPage(currentPage);

		// 每页显示的数据
		int pageSize = 5;
		page.setPageSize(pageSize);
		Map<String, Object> con = new HashMap<String, Object>();
		con.put("minPrice", minPrice);
		con.put("maxPrice", maxPrice);
		con.put("layout", layout);
		con.put("keyword", keyword);

		// 封装总记录数
		int totalCount = houseDealMapper.selectCount1(con);
		page.setTotalCount(totalCount);

		// 封装总页数
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);// 向上取整
		page.setTotalPage(num.intValue());

		map.put("start", (currentPage - 1) * pageSize);
		map.put("size", page.getPageSize());
		map.put("minPrice", minPrice);
		map.put("maxPrice", maxPrice);
		map.put("layout", layout);
		map.put("keyword", keyword);
		// 封装每页显示的数据
		List<HouseInfo> lists = houseDealMapper.findByPage1(map);
		page.setLists(lists);

		return page;
	}

	public Page<HouseInfo> findByPage2(int currentPage, int minPrice, int maxPrice, int layout,String keyword) {
		Map<String, Object> map = new HashMap<String, Object>();
		Page<HouseInfo> page = new Page<HouseInfo>();

		// 封装当前类
		page.setCurrentPage(currentPage);

		// 每页显示的数据
		int pageSize = 5;
		page.setPageSize(pageSize);
		Map<String, Object> con = new HashMap<String, Object>();
		con.put("minPrice", minPrice);
		con.put("maxPrice", maxPrice);
		con.put("layout", layout);
		con.put("keyword", keyword);

		// 封装总记录数
		int totalCount = houseDealMapper.selectCount2(con);
		page.setTotalCount(totalCount);

		// 封装总页数
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);// 向上取整
		page.setTotalPage(num.intValue());

		map.put("start", (currentPage - 1) * pageSize);
		map.put("size", page.getPageSize());
		map.put("minPrice", minPrice);
		map.put("maxPrice", maxPrice);
		map.put("layout", layout);
		map.put("keyword", keyword);
		// 封装每页显示的数据
		List<HouseInfo> lists = houseDealMapper.findByPage2(map);
		page.setLists(lists);

		return page;
	}
	
	public HouseInfo getIdInfo(int id){
		HouseInfo idHouseInfo = new HouseInfo();
		idHouseInfo = houseDealMapper.getIdInfo(id);
		return idHouseInfo;
		
	}
	public void saveDataInDB(int houseId, String userAccount, String houseAgentAccount, Date applyTime,
			String houseAddress, String houseProvince, String houseCity){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("houseId", houseId);
		map.put("userAccount",userAccount);
		map.put("houseAgentAccount",houseAgentAccount);
		map.put("applyTime",applyTime);
		map.put("houseAddress", houseAddress);
		map.put("houseProvince", houseProvince);
		map.put("houseCity", houseCity);
		houseDealMapper.saveDataInDB(map);
	}
	public Page<Renter> findByPage3(int currentPage, int houseId){
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Renter> page = new Page<Renter>();
		// 封装当前类
		page.setCurrentPage(currentPage);

		// 每页显示的数据
		int pageSize = 1;
		page.setPageSize(pageSize);
		Map<String, Object> con = new HashMap<String, Object>();
		con.put("minPrice", houseId);

		// 封装总记录数
		int totalCount = houseDealMapper.selectCount3(con);
		page.setTotalCount(totalCount);

		// 封装总页数
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);// 向上取整
		page.setTotalPage(num.intValue());

		map.put("start", (currentPage - 1) * pageSize);
		map.put("size", page.getPageSize());
		// 封装每页显示的数据
		List<Renter> lists = houseDealMapper.findByPage3(map);
		page.setLists(lists);

		return page;
		
	}
	public void saveDataInDB1(int houseId, String houseAddress,
			String houseProvince, String houseCity,User u){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("houseId", houseId);
		map.put("userAccount",u.getUserAccount());
		map.put("renterTime",new Date());
		map.put("houseAddress", houseAddress);
		map.put("houseProvince", houseProvince);
		map.put("houseCity", houseCity);
		map.put("renterSex", u.getUserSex());
		map.put("renterTele", u.getUserTele());
		houseDealMapper.saveDataInDB1(map);
		
	}

	@Override
	public User getUserById(int userId) {
		// TODO Auto-generated method stub
		return houseDealMapper.getUserById(userId);
	}

	@Override
	public List<Renter> getRenterById(int houseId) {
		// TODO Auto-generated method stub
		return houseDealMapper.getRenterById(houseId);
	}

}
